﻿using MediatR;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
using W.Common;
using W.Product.API;
using W.Product.Domain.Dto;
using W.Product.Domain.Product;
using W.Product.Read.API.Appliaction.Command.ProductCommand;
using W.Product.Repository.Interface;
using static WMSpublic.ParseAPI;

namespace W.Product.Read.API.Appliaction.CommHandler.ProductCommandHandler
{
    public class ProductUnitShowCommandHandler : IRequestHandler<ProductUnitShowCommand, List<ProductUnitDto>>
    {
        private readonly IProductRepository _productRepository;

        public ProductUnitShowCommandHandler(IProductRepository productRepository)
        {
            _productRepository = productRepository;
        }

        public async Task<List<ProductUnitDto>> Handle(ProductUnitShowCommand request, CancellationToken cancellationToken)
        {
            List<Unit> untis = await GetUnitShow(1, 100);
            var res = (from a in await _productRepository.GetAll()
                       join b in untis on
                       a.UnitId equals b.UnitId
                       select new ProductUnitDto
                       {
                           ProductId = a.ProductId,
                           ProductNo = a.ProductNo,
                           InsideNo = a.InsideNo,
                           ProductName = a.ProductName,
                           ProductDesc = a.ProductDesc,
                           ProductPrice = a.ProductPrice,
                           ProductSpec = a.ProductSpec,
                           ProductTypeId = a.ProductTypeId,
                           ProductWeight = a.ProductWeight,
                           ProductWrap = a.ProductWrap,
                           VendorId = a.VendorId,
                           ManuNo = a.ManuNo,
                           GoOnline = a.GoOnline,
                           Online = a.Online,
                           UnitId = a.UnitId,
                           UnitName = b.UnitName,
                           ClientId = a.ClientId,
                           WarehouseId = a.WarehouseId,
                           BinLocationId = a.BinLocationId,
                          
                       }).ToList();
            return res;
        }

        /// <summary>
        /// 单位
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetUnitShow(int pageIndex, int pageSize)
        {
            var token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI0Iiwic2lkIjoiMTg1OTgwNTA4MDc2NTczMDgxNiIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvZ3JvdXBzaWQiOiIiLCJqdGkiOiI5MWU1MzVhMy1kZjVlLTQxNGQtODdkMy0zYjE3MmIzNmRhZWEiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LqO5a2Q5ra1IiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0.9e371kkhbGOGIALMM210Z0Mhka1BhADJFTKFd8w3AJY";
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = "http://10.223.8.18:8000/purchaseRead/api/Unit/Handle?pageIndex=" + pageIndex + "&pageSize=" + pageSize;
            var response = await client.GetAsync(url);
            var json = JsonConvert.DeserializeObject<ApiResultList<Unit>>(response);
            return json.data.list;
        }
        /// <summary>
        /// 单位管理表
        /// </summary>
        public class Unit
        {
            public int UnitId { get; set; }
            /// <summary>
            /// 单位编号
            /// </summary>
            public string? UnitNo { get; set; }
            /// <summary>
            /// 单位名称
            /// </summary>
            public string? UnitName { get; set; }
            /// <summary>
            /// 备注
            /// </summary>
            public string? UnitRemarks { get; set; }
            /// <summary>
            /// 公司ID
            /// </summary>
            public string? office_id { get; set; }
            /// <summary>
            /// 用户ID
            /// </summary>
            public int? user_id { get; set; }

        }

    }
}
